/*
* @Author: Slience
* @Date:   2019-09-23 14:51:28
* @Last Modified by:   sean
* @Last Modified time: 2019-10-15 10:50:52
*/
import api from '@/api'


const state = () => ({
  // 任务一
  // 添加key值
  // url:{
  //    "aosdifuj35435":{
  //       "total":"",
  //       "get":"",
  //       "edit":"",
  //    }
  // },
  // fields:{
  //   "akldfhjladf4565":[],
  // },

  // 任务二
  // ES6语法   规格、详情组件


  // 任务三
  // 仓库管理流程
  // 云仓储基本逻辑
  // 

  //获取数据
  url: {
    // "total": "",
    // "get": "",
    // "edit": ""
  },
  fields: [],
  //筛选条件
  where: {},
  //数据存储
  tableData: {},
  //页数信息
  page: {},
  //总页数
  total: {},
  //页面大小
  size: {},
  //当前页面
  current: {},
  //当前密钥
  mak: "this7",

})

const mutations = {
  //设置当前密钥
  setMak: (state, value) => {
    state.mak = value;
  },
  //设置当前密钥
  setURL: (state,{ key , value }) => {
    state.url[key] = value
  },
  //设置当前筛选条件
  setWhere: (state, {key,value}) => {
    state.where[key] = value
  },
  //设置当数据总条数
  setTotal: (state, {key,value}) => {
    if(JSON.stringify(state.total) == '{}'){
      state.total = {
        [key]:value
      }
    }else{
      var obj = JSON.parse(JSON.stringify(state.total));
      obj[key] = value;
      state.total = obj;
    }
    
  },
  //设置当前筛选条件   每页显示条数
  setSize: (state, {key,value}) => {
    if(JSON.stringify(state.size) == '{}'){
      state.size = {
        [key]:value
      }
    }else{
      var obj = JSON.parse(JSON.stringify(state.size));
      obj[key] = value;
      state.size = obj;
    }

  },
  //修改当前页面
  setCurrent: (state, {key,value}) => {
    if(JSON.stringify(state.current) == '{}'){
      state.current = {
        [key]:value
      }
    }else{
      var obj = JSON.parse(JSON.stringify(state.current));
      obj[key] = value;
      state.current = obj;
    }

  },
  setPage: (state, key) => {
    state.page[key] = [state.current[key], state.size[key]]
  },
  //设置表格数据
  setTableData: (state, {key,value}) => {
    if(JSON.stringify(state.tableData) == '{}'){
      state.tableData = {
        [key]:value
      }
    }else{
      var obj = JSON.parse(JSON.stringify(state.tableData));
      obj[key] = value;
      state.tableData = obj;
    }
  },

  // 修改表格中单元格数据  不提交
  // key : 表格唯一码          { String }
  // index : 表格数据行下标    { Number }
  // field : 数据字段名        { String }
  // value : 修改数据值        { Number String}
  fixTableData:(state,{key,index,field,value}) => {
    state.tableData[key][index][field] = value
  },

  //清空数据
  clearState(state) {
    state.url = "";
    state.where = {};
    state.tableData = [];
  },
}

const actions = {

  //获取表格数据
  getTableData({ dispatch, commit, state },key) {
    let model = state.url[key].get.model;
    let action = state.url[key].get.action;
    api[model][action]({
      where: state.where[key],
      page: state.page[key]
    }).then(response => {
      // console.log("返回数据", response);
      commit('setTableData', {key:key,value:response})
    });
  },
  //修改表格数据
  editTableData({ dispatch, commit, state }, {data,key}) {
    let model = state.url[key].edit.model;
    let action = state.url[key].edit.action;
    api[model][action](data).then(response => {
      console.log("返回数据", response);
      dispatch("getTableData",key);
    });
  },
  //异步操作
  onAsynchronous({
    dispatch,
    commit,
    state
  }, { model, action, key, data, callback }) {
    console.log("异步操作", [model, action, key ,data, callback]);
    api[model][action](data).then(response => {
      console.log("返回数据", response);
      if (typeof func == 'function') {
        console.log("为函数", callback);
        callback();
      }

      //重新获取数据
      dispatch("getTableData",key);
      //重新计算数据
      dispatch("getPageTotal",key);

    });

  },
  //页面跳转
  onCurrentChange({ dispatch, commit, state }, {key,data}) {
    //设置当前页
    commit('setCurrent', {key:key,value:data});
    //设置分页条件
    commit('setPage',key);
    //执行数据获取
    dispatch('getTableData',key);
  },
  //获取页面总条数
  getPageTotal({ commit, state }, key) {
    let model = state.url[key].total.model;
    let action = state.url[key].total.action;
    api[model][action]({ where: state.where[key] }).then(response => {
      console.log("返回总条数", response);
      response.length == 0 ? response = 0 : response;
      commit('setTotal', {key:key,value:response})
    });
  }
}

export default {
  state,
  mutations,
  actions,

}
